﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace SmartSpace.Silverlight
{
    public class CirclePanel : Grid
    {
        //private List<UIElement> m_buttons = new List<UIElement>();

        public CirclePanel()
        {
            this.LayoutUpdated += new EventHandler(CirclePanel_LayoutUpdated);
            this.Loaded += new RoutedEventHandler(CirclePanel_Loaded);
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);

            this.Visibility = System.Windows.Visibility.Collapsed;
        }

        private int m_displayIndex = 0;
        private int m_step = 0;

        void CirclePanel_Loaded(object sender, RoutedEventArgs e)
        {
        }

        public void Hide()
        {
            foreach (UIElement element in this.Children)
            {
                element.Visibility = System.Windows.Visibility.Collapsed;
            }
            this.Visibility = System.Windows.Visibility.Collapsed;
        }

        public void Show()
        {
            m_displayIndex = 0;
            m_step = 2;
            this.Visibility = System.Windows.Visibility.Visible;
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            if (m_displayIndex < this.Children.Count)
            {
                if (m_step > 0)
                {
                    m_step--;
                }
                else
                {
                    double cx = this.ActualWidth / 2;
                    double cy = this.ActualHeight / 2;

                    double angle = Math.PI * 2 / this.Children.Count;
                    double dr = 5.6d * 64;
                    for (int index = 0; index < this.Children.Count; index++)
                    {
                        double a = Math.PI * 1.5d + angle * index;
                        double x = Math.Cos(a) * dr;
                        double y = Math.Sin(a) * dr;
                        FrameworkElement btn = (this.Children[index] as FrameworkElement);
                        btn.Margin = new Thickness(
                            x - btn.ActualWidth / 2, y - btn.ActualWidth / 2, 0, 0);
                        if (index == m_displayIndex)
                        {
                            btn.Visibility = System.Windows.Visibility.Visible;
                            m_displayIndex++;
                            m_step = 1;
                            break;
                        }
                    }
                }
            }
        }

        void CirclePanel_LayoutUpdated(object sender, EventArgs e)
        {
            double cx = this.ActualWidth / 2;
            double cy = this.ActualHeight / 2;

            double dr = cx;
            if (dr > cy)
            {
                dr = cy;
            }
            dr = dr - 64;

            double angle = Math.PI * 2 / this.Children.Count;
            for (int index = 0; index < this.Children.Count; index++)
            {
                double a = Math.PI * 3.0d / 2.0d + angle * index;
                double x = Math.Cos(a) * dr;
                double y = Math.Sin(a) * dr;
                FrameworkElement btn = (this.Children[index] as FrameworkElement);
                btn.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                btn.Margin = new Thickness( cx + x - btn.ActualWidth/2,  cy + y - btn.ActualHeight/2, 0, 0);
            }
        }

    }

    
}
